//
//  LDSTPickerView.m
//  HCNewGoldFinger
//
//  Created by 刘琛 on 2017/8/9.
//  Copyright © 2017年 zjhcsoftios. All rights reserved.
//

#import "LeadPickerView.h"
#import "LeadPickerProvinceModel.h"
#import "iSeeNetworkRequest.h"
#import "NetworkAddress.h"

//领导视图生产环境
#define ServerLDSTAddress @"http://qxisee.zjtelecom.com.cn:9090/appLeader/"

#define LDSTLoaction @"api/district/query" //领导视图地区查询

@interface LeadPickerView ()<UIPickerViewDelegate,UIPickerViewDataSource>

@property (weak, nonatomic) IBOutlet UIPickerView *picker;

//市级别模型
@property (nonatomic, strong) LeadPickerProvinceModel *provinceModel;
@property (nonatomic, assign) NSInteger tempProvinceIndex;
//区级别模型
@property (nonatomic, strong) LeadPickerProvinceModel *cityModel;
@property (nonatomic, assign) NSInteger tempCityIndex;
//分局模型
@property (nonatomic, strong) LeadPickerProvinceModel *countyModel;
@property (nonatomic, assign) NSInteger tempCountyIndex;

@property (nonatomic, strong) NSString *returnString;

@property (nonatomic, strong) NSString *returnId;

// 3.省市选择 2.排名列表2级选择 1.时间选择
@property (nonatomic, assign) NSInteger type;

@property (nonatomic, strong) LeadPickerCityModel *selectModel;

@property (nonatomic, strong) LDSTRankingPMZBModel *topModel;

//中转选中选项点确定后 赋值给select 用于2级转轮选择
@property (nonatomic, assign) NSInteger tempFirstTopIndex;
@property (nonatomic, assign) NSInteger tempSecondTopIndex;
//2级转轮 选中后的选项
@property (nonatomic, assign) NSInteger selectFirstTopIndex;
@property (nonatomic, assign) NSInteger selectSecondTopIndex;

//用于单转轮的记录
@property (nonatomic, assign) NSInteger selectTimeIndex;
@property (nonatomic, assign) NSInteger tempTimeIndex;

@end

@implementation LeadPickerView


-(void)awakeFromNib {
    
    [super awakeFromNib];
    
    _picker.dataSource = self;
    _picker.delegate = self;
    _selectFirstTopIndex = 0;
    _selectSecondTopIndex = 0;
    _selectTimeIndex = 0;
    _tempTimeIndex = 0;
    _type = 3;
}

#pragma mark 现实调用

- (void)showLoaction {
    
    _type = 3;
    
    if (_provinceModel) {
        [_picker reloadAllComponents];
        _selectModel = _provinceModel.items[_provinceModel.selectIndex];
        [_picker selectRow:_provinceModel.selectIndex inComponent:0 animated:YES];
        [self  cityPickerSelectedAndNameArray:_provinceModel.names];
        if (_cityModel) {
            _selectModel = _cityModel.items[_cityModel.selectIndex];
            [_picker selectRow:_cityModel.selectIndex inComponent:1 animated:YES];
            [self  cityPickerSelectedAndNameArray:_cityModel.names];
            if (_countyModel) {
                _selectModel = _countyModel.items[_countyModel.selectIndex];
                [_picker selectRow:_countyModel.selectIndex inComponent:2 animated:YES];
                [self  cityPickerSelectedAndNameArray:_countyModel.names];
                
            }
        }
    }else {
        [self getDataFromServer:@"99"];
    }
    

    
}

- (void)pickerViewResetTime {
    _selectTimeIndex = 0;
}

- (void)setTimeArray:(NSArray *)timeArray {
    _timeArray = timeArray;
    _type = 1;
    if (_selectTimeIndex == 0) {
        _returnString = _timeArray[0];
    }else {
        _returnString = _timeArray[_selectTimeIndex];
    }
    [_picker reloadAllComponents];
    [_picker selectRow:_selectTimeIndex inComponent:0 animated:YES];
}

- (void)setPmzbArray:(NSArray *)pmzbArray {
    _pmzbArray = pmzbArray;
    _type = 2;
    if (_selectFirstTopIndex == 0 && _selectSecondTopIndex == 0) {
        _topModel = _pmzbArray[0];
        _returnId = _topModel.child[0].configCode;
        _tempFirstTopIndex = _selectFirstTopIndex;
        _tempSecondTopIndex = _selectSecondTopIndex;
    }else {
        _topModel = _pmzbArray[_selectFirstTopIndex];
        _returnId = _topModel.child[_selectSecondTopIndex].configCode;
    }
    _returnString = [NSString stringWithFormat:@"%@-%@",_topModel.configName, _topModel.child[_selectSecondTopIndex].configName];
    [_picker reloadAllComponents];
    [_picker selectRow:_selectFirstTopIndex inComponent:0 animated:YES];
    [_picker selectRow:_selectSecondTopIndex inComponent:1 animated:YES];
    
}

#pragma mark picker选择器

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    
    return _type;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (_type == 3) {
        if (component == 0) {
            return _provinceModel.items.count;
        }else if (component == 1) {
            return _cityModel.items.count;
        }else if (component == 2) {
            return _countyModel.items.count;
        }
    }else if (_type == 2) {
        
        if (component == 0) {
            return _pmzbArray.count;
        }else if (component == 1) {
            return _topModel.child.count;
        }
        
        
    }else if (_type == 1) {
        return _timeArray.count;
    }
    return 10;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (_type == 3) {
        if (component == 0) {
            return _provinceModel.items[row].areaName;
        }else if (component == 1) {
            return _cityModel.items[row].areaName;
        }else if (component == 2) {
            return _countyModel.items[row].areaName;
        }
    }else if (_type == 2) {
        if (component == 0) {
            LDSTRankingPMZBModel *model =  _pmzbArray[row];
            return model.configName;
        }else if (component == 1) {
            return _topModel.child[row].configName;
        }
    }else if (_type == 1) {
        return _timeArray[row];
    }
    
    return @"杭州";
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (_type == 3) {
        
        if (component == 0) {
            //市级被选中的时候
            _selectModel = _provinceModel.items[row];
            [self getDataFromServer:_provinceModel.items[row].areaId];
            _tempProvinceIndex = row;
            [self  cityPickerSelectedAndNameArray:_provinceModel.names];
        }else if (component == 1) {
            //区级被选中的时候
            if (_cityModel) {
                _selectModel = _cityModel.items[row];
                [self getDataFromServer:_cityModel.items[row].areaId];
                _tempCityIndex = row;
                [self  cityPickerSelectedAndNameArray:_cityModel.names];
            }
        }else if (component == 2) {
            if (_countyModel) {
                _selectModel = _countyModel.items[row];
                [self  cityPickerSelectedAndNameArray:_countyModel.names];
                _tempCountyIndex = row;
            }
        }
    }else if (_type == 2) {
        //2级选择
        if (component == 0) {
            _topModel = _pmzbArray[row];
            [pickerView reloadAllComponents];
            [_picker selectRow:0 inComponent:1 animated:YES];
            _returnId = _topModel.child[0].configCode;
            _returnString = [NSString stringWithFormat:@"%@-%@",_topModel.configName, _topModel.child[0].configName];
            _tempFirstTopIndex = row;
            _tempSecondTopIndex = 0;
            
        }else if (component == 1) {
            _returnId = _topModel.child[row].configCode;
            _returnString = [NSString stringWithFormat:@"%@-%@",_topModel.configName, _topModel.child[row].configName];
            _tempSecondTopIndex = row;
        }
        
    }else if (_type == 1) {
        //1级选择
        _returnString = _timeArray[row];
        _returnId = @"";
        _tempTimeIndex = row;
    }
}


- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        // Setup label properties - frame, font, colors etc
        //adjustsFontSizeToFitWidth property to YES
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont boldSystemFontOfSize:18]];
//        pickerLabel.textColor = [UIColor colorWithHexString:@"1B82D2"];
    }
    // Fill the label text here
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}


#pragma mark 网络请求

- (void)getDataFromServer:(NSString *) areaId{
    

    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    
    [params setObject:areaId forKey:@"areaId"];
    
    [iSeeNetworkRequest postWithHeaderUrl:kFormat(@"%@%@", ServerLDSTAddress,LDSTLoaction) params:params success:^(id object) {
        LeadPickerProvinceModel *model = [LeadPickerProvinceModel mj_objectWithKeyValues:object[@"body"]];
        if (model) {
            if (model.names.count == 1) {
                
                self.provinceModel = model;
                self.cityModel = nil;
                self.countyModel = nil;
                [self.picker reloadAllComponents];
                
                //如果是市级权限只能看到一个市的时候 直接请求第二列区列表
                if (self.provinceModel.items.count == 1) {
                    [self getDataFromServer:self.provinceModel.items[0].areaId];
                    self.selectModel = self.provinceModel.items[0];
                    self.returnString = self.provinceModel.items[0].areaName;
                    self.returnId = self.provinceModel.items[0].areaId;
                    
                }else if (self.provinceModel.items.count > 1){
                    LeadPickerCityModel *newModel = [[LeadPickerCityModel alloc] init];
                    newModel.areaName = @"全省";
                    newModel.areaId = @"99";
                    NSMutableArray *array = [NSMutableArray arrayWithObject:newModel];
                    [array addObjectsFromArray:self.provinceModel.items];
                    self.provinceModel.items = array;
                    self.returnString = self.provinceModel.items[0].areaName;
                    self.returnId = self.provinceModel.items[0].areaId;
                }
                [self.picker selectRow:0 inComponent:0 animated:YES];
            }else if (model.names.count == 2) {
                self.cityModel = model;
                
                //增加全市
                if (self.cityModel.items.count > 1){
                    LeadPickerCityModel *newModel = [[LeadPickerCityModel alloc] init];
                    newModel.areaName = @"全市";
                    newModel.areaId = areaId;
                    NSMutableArray *array = [NSMutableArray arrayWithObject:newModel];
                    [array addObjectsFromArray:self.cityModel.items];
                    self.cityModel.items = array;
                    self.tempCityIndex = 0;
                    
                }else {
                    [self getDataFromServer:self.cityModel.items[0].areaId];
                    self.selectModel = self.cityModel.items[0];
                    self.returnString = self.cityModel.items[0].areaName;
                    self.returnId = self.cityModel.items[0].areaId;
                }
                self.countyModel = nil;
                [self.picker reloadAllComponents];
                [self.picker selectRow:0 inComponent:1 animated:YES];
                
            }else if (model.names.count == 3) {
                self.countyModel = model;
                if (self.countyModel.items.count > 1 || self.countyModel.items.count == 0) {
                    LeadPickerCityModel *newModel = [[LeadPickerCityModel alloc] init];
                    newModel.areaName = @"全区县";
                    newModel.areaId = areaId;
                    NSMutableArray *array = [NSMutableArray arrayWithObject:newModel];
                    [array addObjectsFromArray:self.countyModel.items];
                    self.countyModel.items = array;
                    self.tempCountyIndex = 0;
                }else {
                    [self getDataFromServer:self.countyModel.items[0].areaId];
                    self.selectModel = self.countyModel.items[0];
                    self.returnString = self.countyModel.items[0].areaName;
                    self.returnId = self.countyModel.items[0].areaId;
                }
                [self.picker reloadAllComponents];
                [self.picker selectRow:0 inComponent:2 animated:YES];
                
            }
            
        }
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark 处理城市选择的选中事件

- (void)cityPickerSelectedAndNameArray:(NSArray *) names;{
    _returnId = _selectModel.areaId;
    NSMutableArray *array = [NSMutableArray array];
    [array addObjectsFromArray:names];
    [array addObject:_selectModel.areaName];
    [array removeObjectAtIndex:0];
    if ([_selectModel.areaName isEqualToString:@"全市"] || [_selectModel.areaName isEqualToString:@"全区县"]) {
        [array removeLastObject];
    }
    _returnString = array.lastObject;
}


#pragma mark 确定 取消按钮点击事件
- (IBAction)tureButtonAction:(UIButton *)sender {
    
    if ([_delegate respondsToSelector:@selector(pickerView:AndSelected:AndTitle:AndType:)]) {
        [_delegate pickerView:self AndSelected:_returnId AndTitle:_returnString AndType:_type];
//        [_delegate pickerViewSelected:_returnId AndTitle:_returnString AndType:_type];
    }
    
    if (_type == 3) {
        _provinceModel.selectIndex = _tempProvinceIndex;
        _cityModel.selectIndex = _tempCityIndex;
        _countyModel.selectIndex = _tempCountyIndex;
    }else if (_type == 2) {
        _selectFirstTopIndex = _tempFirstTopIndex;
        _selectSecondTopIndex = _tempSecondTopIndex;
    }else if (_type == 1) {
        _selectTimeIndex = _tempTimeIndex;
    }
    
    [self removeFromSuperview];

}

- (void)clearAll {
    _topModel = nil;
    _selectModel = nil;
    _provinceModel = nil;
    _cityModel = nil;
    _countyModel = nil;
    [_picker reloadAllComponents];
}


- (IBAction)cannelButtonAction:(id)sender {
//    [self clearAll];
    [self removeFromSuperview];
}




- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    [self clearAll];
    [self removeFromSuperview];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
